﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DAO_DTO_WS.DTO;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace DAO_DTO_WS.DAO
{
    public class DonHangDAO : Abstract
    {
        public static List<DonHangDTO> layDonHangTheoMaThanhVien(int maThanhVien)
        {
            List<DonHangDTO> dsDonHang = new List<DonHangDTO>();
            SqlConnection connection = KetNoi();
            string sqlstring = "SELECT * from DONHANG where MaThanhVien =@MaThanhVien";
            try
            {
                SqlCommand command = new SqlCommand(sqlstring, connection);
                command.Parameters.AddWithValue("@MaThanhVien", maThanhVien);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    DonHangDTO donhang = new DonHangDTO();
                    donhang.MaDonHang = (int)reader[0];
                    donhang.MaThanhVien = maThanhVien;
                    donhang.MaSanPham = (int)reader[2];
                    donhang.SoLuong = (int)reader[3];
                    donhang.NgayMua = (DateTime)reader[4];
                    donhang.TongTien = (Int64)reader[5];
                    donhang.MaLoaiThanhToan = (int)reader[6];
                    donhang.TinhTrang = (int)reader[7];
                    dsDonHang.Add(donhang);
                }
            }
            catch
            {
                dsDonHang = new List<DonHangDTO>();
            }
            finally
            {
                if ((connection != null) && (connection.State == ConnectionState.Open))
                    connection.Close();
            }
            return dsDonHang;
        }
        public static List<DonHangDTO> layDonHangTheoMaSanPham(int maSanPham)
        {
            List<DonHangDTO> dsDonHang = new List<DonHangDTO>();
            SqlConnection connection = KetNoi();
            string sqlstring = "SELECT * from DONHANG where MaSanPham =@MaSanPham";
            try
            {
                SqlCommand command = new SqlCommand(sqlstring, connection);
                command.Parameters.AddWithValue("@MaSanPham", maSanPham);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    DonHangDTO donhang = new DonHangDTO();
                    donhang.MaDonHang = (int)reader[0];
                    donhang.MaThanhVien = (int)reader[1];
                    donhang.MaSanPham = maSanPham;
                    donhang.SoLuong = (int)reader[3];
                    donhang.NgayMua = (DateTime)reader[4];
                    donhang.TongTien = (Int64)reader[5];
                    donhang.MaLoaiThanhToan = (int)reader[6];
                    donhang.TinhTrang = (int)reader[7];
                    dsDonHang.Add(donhang);
                }
            }
            catch
            {
                dsDonHang = new List<DonHangDTO>();
            }
            finally
            {
                if ((connection != null) && (connection.State == ConnectionState.Open))
                    connection.Close();
            }
            return dsDonHang;
        }
        public static bool ThemDonHang(DonHangDTO donhang)
        {
            bool ketqua = false;
            SqlConnection connection = KetNoi();
            String SqlString = "INSERT INTO DONHANG(MaThanhVien,MaSanPham,SoLuong,NgayMua,TongTien,MaLoaiThanhToan,TinhTrang) VALUES " +
                 "(@MaThanhVien,@MaSanPham,@SoLuong,@NgayMua,@TongTien,@MaLoaiThanhToan,@TinhTrang)";
            try
            {
                SqlCommand command = new SqlCommand(SqlString, connection);
                command.Parameters.AddWithValue("@MaThanhVien", donhang.MaThanhVien);
                command.Parameters.AddWithValue("@MaSanPham", donhang.MaSanPham);
                command.Parameters.AddWithValue("@SoLuong", donhang.SoLuong);
                command.Parameters.AddWithValue("@NgayMua", donhang.NgayMua);
                command.Parameters.AddWithValue("@TongTien", donhang.TongTien);
                command.Parameters.AddWithValue("@MaLoaiThanhToan", donhang.MaLoaiThanhToan);
                command.Parameters.AddWithValue("@TinhTrang", donhang.TinhTrang);
                int rows = command.ExecuteNonQuery();
                if (rows == 1)
                    ketqua = true;
            }
            catch
            {
                ketqua = false;
            }
            finally
            {
                if ((connection != null) && (connection.State == ConnectionState.Open))
                    connection.Close();
            }
            return ketqua;
        }
    }
}
